package com.io;

import java.util.ArrayList;

public class Set {

    private ArrayList<Integer> ivalues;
    private ArrayList<Character> cvalues;

    private enum SetType {INTEGER, CHAR};

    private SetType type;

    public static Set CreateSetInteger(int... ints) {
        Set s = new Set();
        s.ivalues = new ArrayList<Integer>();
        for(int i : ints) {
            s.ivalues.add(i);
        }
        s.type = SetType.INTEGER;
        return s;
    }

    public static Set CreateSetCharacter(char... chrs) {
        Set s = new Set();
        s.cvalues = new ArrayList<Character>();
        for(char c : chrs) {
            s.cvalues.add(c);
        }
        s.type = SetType.CHAR;
        return s;
    }

    @Override
    public String toString() {
        String str = new String();
        if(this.type == SetType.INTEGER) {
            for(Integer i : this.ivalues) {
                if(str.length() != 0) {
                    str += ',';
                }
                str += i.toString();
            }
        }
        else {
            for(Character c : this.cvalues) {
                if(str.length() != 0) {
                    str += ',';
                }
                str += c.charValue();
            }
        }
        return str;
    }

    public static int IN(int value, Set s) {
        if(s.ivalues.contains(value)) {
            return 1;
        }
        return 0;
    }

    public static int IN(char value, Set s) {
        if(s.cvalues.contains(value)) {
            return 1;
        }
        return 0;
    }

    public static Set UnSet(Set s1, Set s2) {
        Set s = new Set();
        if(s1.type == SetType.INTEGER) {
            s.ivalues = new ArrayList<Integer>();
            for (int i : s1.ivalues) {
                s.ivalues.add(i);
            }
            for (int i : s2.ivalues) {
                if (!s1.ivalues.contains(i)) {
                    s.ivalues.add(i);
                }
            }
        }
        else {
            s.cvalues = new ArrayList<Character>();
            for (char c : s1.cvalues) {
                s.cvalues.add(c);
            }
            for (char c : s2.cvalues) {
                if (!s1.cvalues.contains(c)) {
                    s.cvalues.add(c);
                }
            }
        }
        return s;
    }

    public static Set DifSet(Set s1, Set s2) {
        Set s = new Set();
        if(s1.type == SetType.INTEGER) {
            s.ivalues = new ArrayList<Integer>();
            for (int i : s1.ivalues) {
                if (!s2.ivalues.contains(i)) {
                    s.ivalues.add(i);
                }
            }
        }
        else {
            s.cvalues = new ArrayList<Character>();
            for (char c : s1.cvalues) {
                if (!s2.cvalues.contains(c)) {
                    s.cvalues.add(c);
                }
            }
        }
        return s;
    }

    public static Set IntSet(Set s1, Set s2) {
        Set s = new Set();
        if(s1.type == SetType.INTEGER) {
            s.ivalues = new ArrayList<Integer>();
            for (int i : s1.ivalues) {
                if (s2.ivalues.contains(i)) {
                    s.ivalues.add(i);
                }
            }
        }
        else {
            s.cvalues = new ArrayList<Character>();
            for (char c : s1.cvalues) {
                if (s2.cvalues.contains(c)) {
                    s.cvalues.add(c);
                }
            }
        }
        return s;
    }

    public static Set SdifSet(Set s1, Set s2) {
        Set s = new Set();
        if(s1.type == SetType.INTEGER) {
            s.ivalues = new ArrayList<Integer>();
            for (int i : s1.ivalues) {
                if (!s2.ivalues.contains(i)) {
                    s.ivalues.add(i);
                }
            }
            for (int i : s2.ivalues) {
                if (!s1.ivalues.contains(i)) {
                    s.ivalues.add(i);
                }
            }
        }
        else {
            s.cvalues = new ArrayList<Character>();
            for (char c : s1.cvalues) {
                if (!s2.cvalues.contains(c)) {
                    s.cvalues.add(c);
                }
            }
            for (char c : s2.cvalues) {
                if (!s1.cvalues.contains(c)) {
                    s.cvalues.add(c);
                }
            }
        }
        return s;
    }
}
